package com.coship.vasms.adstorm.exception;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.dom4j.tree.DefaultElement;

import com.coship.vasms.adstorm.common.log.JLogger;
import com.coship.vasms.adstorm.common.log.LoggerFactory;

public class ExceptionConfigCache {

	private static JLogger logger = LoggerFactory
			.getLogger(ExceptionConfigCache.class);

	private static ExceptionConfigCache instance;
	// 缓存的内
	private static Map<String, Error> cache = new HashMap<String, Error>();
	// 错误消息配置文件名称
	private static final String ERROR_MESSAGE_FILE_NAME = "error_massage.xml";

	// 私有化F
	private ExceptionConfigCache() {
		init();
	}

	public static ExceptionConfigCache getInstance() {
		if (instance == null) {
			synchronized (ExceptionConfigCache.class) {// double check
				if (instance == null) {
					instance = new ExceptionConfigCache();
				}
			}
		}
		return instance;
	}

	private void init() {
		SAXReader reader = new SAXReader();
		try {
			Document document = reader.read(ExceptionConfigCache.class
					.getClassLoader().getResourceAsStream(
							ERROR_MESSAGE_FILE_NAME));
			parse(document.getRootElement());
		} catch (DocumentException e) {
			logger.error("解析error_massage.xml文件出现错误,cause:", e);
			// 打堆栈到控制�?,防止logger没有被正确初始化
			e.printStackTrace();
		}
	}

	private void parse(Element rootElement) {
		if (rootElement == null) {
			throw new RuntimeException("error_massage.xml配置文件错误");
		}
		List<DefaultElement> errors = rootElement.elements("error");
		if (errors != null && errors.size() > 0) {
			for (DefaultElement item : errors) {
				Error error = new Error();
				error.setCode(item.elementText("code"));
				if (error.getCode() == null
						|| error.getCode().trim().length() == 0) {
					throw new RuntimeException(
							"error_massage.xml配置文件的配置信息错误,原因:存在code为空的配置项,请检查error_massage.xml文件");
				}
				error.setText(item.elementText("text"));
				error.setDescription(item.elementText("description"));
				cache.put(error.getCode(), error);
			}
		}

	}

	/**
	 * 
	 * Description : 读描述信
	 * 
	 * @param errorCode
	 * @return
	 * 
	 */
	public String getErrorDesc(String errorCode) {
		Error error = getError(errorCode);
		if (error != null) {
			return error.getDescription();
		}
		return null;

	}

	/**
	 * 
	 * Description : 读异常信
	 * 
	 * @param errorCode
	 * @return
	 * 
	 */
	public String getErrorText(String errorCode) {
		Error error = getError(errorCode);
		if (error != null) {
			return error.getText();
		}
		return null;

	}

	/**
	 * 
	 * Description : 读异常配置信
	 * 
	 * @param errorCode
	 * @return
	 * 
	 */
	public Error getError(String errorCode) {
		if (errorCode == null || errorCode.trim().length() == 0) {
			return null;
		}
		return cache.get(errorCode);
	}

	/**
	 * Description : <Detail Description for method>
	 * 
	 * @param args
	 * 
	 */

	public static void main(String[] args) { // TODO Auto-generated method

		System.out.println(ExceptionConfigCache.getInstance().getErrorText(
				"template.name.repeat"));
	}


}
